﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DealKhuyenMaiV2.com.Models;
using PagedList;

namespace DealKhuyenMaiV2.com.Controllers
{
    [DealKhuyenMaiV2.com.Controllers.AccountController.AdminAuthorize]
    public class ProvinceManagerController : BaseController
    {
        //
        // GET: /ProvinceManager/

        public ViewResult Index()
        {
            var provinces = DbConText.Provinces;
            int pageSize = 10;
            ViewBag.Methol = "Index";
            return View(provinces.ToList().ToPagedList(1, pageSize));
        }
        [HttpPost]
        public PartialViewResult Index(int? page)
        {
            var Provinces = DbConText.Provinces;
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Index";
            return PartialView("_ListProvince", Provinces.ToList().ToPagedList(pageNumber, pageSize));
        }
        [HttpPost]
        public PartialViewResult Search(int? page, string KeyWord, string Display)
        {
            var provinces = DbConText.Provinces.Where(
                p => (p.NameProvince.IndexOf(KeyWord) > -1
                    || KeyWord.IndexOf(p.NameProvince) > -1
                    ));
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Search";
            ViewBag.Display = Display;
            ViewBag.KeyWord = KeyWord;
            return PartialView("_ListProvince", provinces.ToList().ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /ProvinceManager/Details/5

        public ViewResult Details(int id)
        {
            Province province = DbConText.Provinces.Single(p => p.IDProvince == id);
            return View(province);
        }

        //
        // GET: /ProvinceManager/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ProvinceManager/Create

        [HttpPost]
        public ActionResult Create(Province province)
        {
            if (ModelState.IsValid)
            {
                DbConText.Provinces.AddObject(province);
                DbConText.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(province);
        }

        //
        // GET: /ProvinceManager/Edit/5

        public ActionResult Edit(int id)
        {
            Province province = DbConText.Provinces.Single(p => p.IDProvince == id);
            return View(province);
        }

        //
        // POST: /ProvinceManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Province province)
        {
            Province _province = DbConText.Provinces.Single(p => p.IDProvince == id);
            {
                _province.NameProvince = province.NameProvince;

            }
            if (ModelState.IsValid)
            {
                DbConText.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(province);
        }

        //
        // GET: /ProvinceManager/Delete/5

        public ActionResult Delete(int id)
        {
            Province province = DbConText.Provinces.Single(p => p.IDProvince == id);
            return View(province);
        }

        //
        // POST: /ProvinceManager/Delete/5

        //[HttpPost, ActionName("Delete")]
        //public ActionResult DeleteConfirmed(int id)
        //{
        //    Province province = DbConText.Provinces.Single(p => p.IDProvince == id);
        //    DbConText.Provinces.DeleteObject(province);
        //    DbConText.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        [HttpPost, ActionName("Delete")]
        public PartialViewResult DeleteConfirmed(int? page, int id)
        {
            Province province = DbConText.Provinces.Single(p => p.IDProvince == id);
            DbConText.Provinces.DeleteObject(province);
            DbConText.SaveChanges();

            var Provinces = DbConText.Provinces;
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Index";
            return PartialView("_ListProvince", Provinces.ToList().ToPagedList(pageNumber, pageSize));

        }
    }
}